package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

public class UserDetails {
        private String username;
        private String password;
        private boolean firstTime;

        public String loadUserFiles(){
                System.out.println("username:"+username);
                System.out.println("password:"+password);
                File sourceDir = new File(NewGmailUser.getSelectedContextPath());
                //authenticating user details
                try{
                        Properties props = new Properties();
                        System.out.println("UserDetails:LoadUserFiles:"+NewGmailUser.getSelectedContextPath()+IDEConstants.SOURCE_FOLDER+File.separator+IDEConstants.USER_PROPS_FILE);
                        props.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+IDEConstants.SOURCE_FOLDER+File.separator+IDEConstants.USER_PROPS_FILE));
                        System.out.println("props.getProperty(username):"+props.getProperty(username));
                        
                        Set keySet = props.keySet();
                        if(!keySet.contains(username)){
                                FacesMessage message = new FacesMessage("User Name incorrect");
                                FacesContext.getCurrentInstance().addMessage("User Name incorrect", message);
                                return "userdetailspage";
                                
                        }else if(props.getProperty(username)!=null && !props.getProperty(username).equals(password)){
                                System.out.println("with in password check");
                                FacesMessage message = new FacesMessage("Password incorrect for User:"+username);
                                FacesContext.getCurrentInstance().addMessage("Password incorrect for User:"+username, message);
                                return "userdetailspage";
                        }
                        if(props.getProperty(username).equals("admin")){
                                System.out.println("loadUserFiles goToAdminPage ");
                                return "goToAdminPage";
                        }
                        if(props.getProperty(username).equals("eclipse23")){
                            System.out.println("loadUserFiles goToAdminPage ");
                            System.out.println("((((((((((::::::"+"cmd /c "+ sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\eclipse\\eclipse.exe -data "+ sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\testworkspace");
                            //Process p = new ProcessBuilder(sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\eclipse\\eclipse.exe -data "+ sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\testworkspace").start();
                           // Process p1=Runtime.getRuntime().exec("cmd /c "+ sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\eclipse\\eclipse.exe -data "+ sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\testworkspace");
                            String command[] = {"cmd.exe","/c",sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\eclipse\\eclipse.exe -data "+ sourceDir+"\\"+IDEConstants.USERS_FOLDER+"\\testworkspace"};

                			/* executing the command with environments set. */
                			Process pro = Runtime.getRuntime().exec(command);
                			pro.waitFor();
                            return null;
                        }
                }catch(Exception e){
                        e.printStackTrace();
                }
                System.out.println("sourceDir:"+sourceDir);
                File srcFolder = new File(sourceDir+IDEConstants.USERS_FOLDER+File.separator+username);
                if(!srcFolder.isDirectory()){
                        srcFolder.mkdir();
                }
                try{
                copyDirectory(new File(NewGmailUser.getSelectedContextPath()+IDEConstants.SOURCE_FOLDER),new File(NewGmailUser.getSelectedContextPath()+IDEConstants.USERS_FOLDER+File.separator+username));
                firstTime=true;
                }catch(Exception e){
                        e.printStackTrace();
                }
                return "success";
        }
        public String forgetPassword(){
                return "gotoForgetPasword";
        }
           void copyDirectory(File sourceLocation , File targetLocation) throws IOException {
            if (sourceLocation.isDirectory()) {
                if (!targetLocation.exists()) {
                    targetLocation.mkdir();
                }
                String[] createrecent = targetLocation.list(new FilenameFilter() {
                    public boolean accept(File arg0, String arg1) {
                            // TODO Auto-generated method stub
                            return arg1.equals("currentSessionFiles.properties");
                    }
                });
                if(createrecent.length==0){
	                //String[] children = sourceLocation.list();
	                String[] children = sourceLocation.list(new FilenameFilter() {
	                                        String jarName ="jars";
	                                        @Override
	                                        public boolean accept(File arg0, String arg1) {
	                                                // TODO Auto-generated method stub
	                                                return !arg1.equals("jars") && !arg1.equals("forGetPassword.properties") && 
	                                                	   !arg1.equals("UserDetails.properties");
	                                        }
	                                });
	                for (int i=0; i<children.length; i++) {
	                    copyDirectory(new File(sourceLocation, children[i]),
	                            new File(targetLocation, children[i]));
	                }
                }
            } else {

                InputStream in = new FileInputStream(sourceLocation);
                OutputStream out = new FileOutputStream(targetLocation);

                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            }
        }
        public String goToNewUser(){
                return "newuser";
        }
        public String getUsername() {
                return username;
        }
        public void setUsername(String username) {
                this.username = username;
        }
        public String getPassword() {
                return password;
        }
        public void setPassword(String password) {
                this.password = password;
        }
                public boolean isFirstTime() {
                        return firstTime;
                }
                public void setFirstTime(boolean firstTime) {
                        this.firstTime = firstTime;
                }
}
